package com.fancy.learncenter.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.fancy.learncenter.adapter.GraphicAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.DimenUtil;
import com.fancy.learncenter.utils.PermissionUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.view.CustomProgressDialog;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TResult;
import com.superservice.lya.R;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ReleaseGraphicActivity extends TakePhotoActivity {

    private static final int ACCESS_LOCATION_RESULT_CODE = 100100;

    @Bind(R.id.next)
    TextView next;
    @Bind(R.id.et_title)
    EditText etTitle;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.yibaocun)
    TextView yibaocun;
    @Bind(R.id.ll)
    LinearLayout ll;
    @Bind(R.id.tv_location)
    TextView tvLocation;
    @Bind(R.id.biaoqing)
    ImageView biaoqing;
    @Bind(R.id.tupian)
    ImageView tupian;
    @Bind(R.id.paizhao)
    ImageView paizhao;
    @Bind(R.id.weizhi)
    ImageView weizhi;
    @Bind(R.id.jianpan)
    ImageView jianpan;
    @Bind(R.id.bottom_bar)
    RelativeLayout bottomBar;
    @Bind(R.id.et_content)
    EditText etContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_releasegraphic);
        ButterKnife.bind(this);
        next.setText("完成");
        initRecycle();
    }


    ArrayList<String> itemDatas;
    GraphicAdapter graphicAdapter;


    ArrayList<String> picDatas;

    private void initRecycle() {
        picDatas = new ArrayList<>();
        itemDatas = new ArrayList<>();
        graphicAdapter = new GraphicAdapter(this, itemDatas);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setAdapter(graphicAdapter);
        graphicAdapter.setPicCallBack(new GraphicAdapter.SetPicCallBack() {
            @Override
            public void picCallBack(int focusPosition, int position) {

            }
        });

        takePhoto = getTakePhoto();

        takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);

    }

    TakePhoto takePhoto;

    @OnClick({R.id.back, R.id.next, R.id.tupian, R.id.weizhi, R.id.paizhao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:

                finish();
//                LiZhiDialog.show(this, R.mipmap.bg_tuxing, "退出前，您需要保存吗？",
//                        true, "不保存", "保存", new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                finish();
//                                LiZhiDialog.dismissDialog();
//                            }
//                        }, new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                finish();
//                                LiZhiDialog.dismissDialog();
//                            }
//                        });
                break;
            case R.id.next:
                submit();
                break;
            case R.id.tv_location:
                tvLocation.setVisibility(View.GONE);
                tvLocation.setText("");
                break;
            case R.id.delete:
//                imageTemp.setVisibility(View.GONE);
                break;
            case R.id.tuozhuai:
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DimenUtil.dip2px(180));
                layoutParams.setMargins(0, 0, 0, DimenUtil.dip2px(-90));//4个参数按顺序分别是左上右下
//                image.setLayoutParams(layoutParams);
                break;
            case R.id.biaoqing:
                break;
            case R.id.tupian:
                Uri imageUri = Uri.fromFile(Utils.getLiZiPicFile());
//                takePhoto.onPickFromGalleryWithCrop(imageUri, Utils.getCutPicCropOptions());
                takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
                takePhoto.onPickFromGallery();
                break;
            case R.id.paizhao:
                takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
                takePhoto.onPickFromCaptureWithCrop(Utils.getCutPicPath(this), getCropOptions());
                break;
            case R.id.weizhi:
                graphicAdapter.notifyDataSetChanged();
                PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_FINE_LOCATION, mPermissionGrant);
                break;
            case R.id.jianpan:
                closeKeyBord(etTitle, this);
                break;
        }
    }

    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
                    //权限被允许（如果被拒绝的话就会提示去设置中打开权限）
                    startActivityForResult(new Intent(ReleaseGraphicActivity.this, MapLocationActivity.class), ACCESS_LOCATION_RESULT_CODE);
                    break;
            }
        }
    };

    /**
     * 定位权限授权回调
     */
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    }

    /**
     * 获取地图定位后回调
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ACCESS_LOCATION_RESULT_CODE && resultCode == RESULT_OK) {
            String locationStr = data.getStringExtra(Constant.LOCATION_STRING);
            if (!TextUtils.isEmpty(locationStr)) {
                tvLocation.setVisibility(View.VISIBLE);
                tvLocation.setText(locationStr);
            }
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 关闭键盘
     */
    public static void closeKeyBord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }


    private CropOptions getCropOptions() {
        CropOptions.Builder builder = new CropOptions.Builder();
        builder.setWithOwnCrop(true);
        builder.setAspectX(4);
        builder.setAspectY(3);
        return builder.create();
    }

    private CompressConfig getCompressConfig() {

        CompressConfig compressConfig = new CompressConfig.Builder()
                .setMaxSize(100 * 1024).setMaxPixel(800).create();
        return compressConfig;
    }

    ArrayList<String> fileDatas = new ArrayList<>();

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        if (result.getImage() != null && !TextUtils.isEmpty(result.getImage().getCompressPath())) {
            itemDatas.add(result.getImage().getCompressPath());

            picDatas.add(result.getImage().getCompressPath());
            graphicAdapter.editStr.add("");

            graphicAdapter.notifyDataSetChanged();

            fileDatas.add(result.getImage().getCompressPath());
        }
    }

    /**
     * 通过imageWidth 的宽度，自动适应高度
     * * @param simpleDraweeView view
     * * @param imagePath  Uri
     * * @param imageWidth width
     */
    public static void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, final int imageWidth) {
        final ViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams();
        ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                if (imageInfo == null) {
                    return;
                }
                int height = imageInfo.getHeight();
                int width = imageInfo.getWidth();
                layoutParams.width = imageWidth;
                layoutParams.height = (int) ((float) (imageWidth * height) / (float) width);
                simpleDraweeView.setLayoutParams(layoutParams);
            }

            @Override
            public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                throwable.printStackTrace();
            }
        };
        DraweeController controller = Fresco.newDraweeControllerBuilder().setControllerListener(controllerListener).setUri(Uri.parse(imagePath)).build();
        simpleDraweeView.setController(controller);
    }


    Dialog dialog;
    boolean isSubmit = false;

    private void disDia() {

        isSubmit = false;
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    private void submit() {

        if (isSubmit) {
            return;
        } else {
            isSubmit = true;
            if (dialog == null) {
                dialog = CustomProgressDialog.creatRequestDialog(this);
            }
            dialog.show();

        }

        StringBuilder content = new StringBuilder();

        if (!TextUtils.isEmpty(etContent.getText().toString())) {
            content.append(etContent.getText().toString());
        }

        for (int i = 0; i < graphicAdapter.editStr.size(); i++) {


            if (!TextUtils.isEmpty(graphicAdapter.editText.get(i))) {

            }

            content.append(graphicAdapter.editStr.get(i).toString() + "{" + i + "}");
        }


        Map maps = new HashMap();

        List<String> datas = new ArrayList();
        for (int i = 0; i < picDatas.size(); i++) {

            if (TextUtils.isEmpty(Utils.imageToBase64(picDatas.get(i)))) {
                datas.add("");
            } else {
                datas.add("data:image/jpeg;base64," + Utils.imageToBase64(picDatas.get(i)));
            }

        }

        maps.put("title", etTitle.getText().toString());

        maps.put("content", content.toString());


        String[] array = datas.toArray(new String[0]);


        HttpMehtod.getInstance().releaseTz(maps, array, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean stringBaseDataBean) {
                ToastUtil.show("发布成功");
                disDia();
                finish();
            }

            @Override
            public void onFail(int errorCode) {
                disDia();
            }

        });

    }


    private void submitData() {
        Map maps = new HashMap<>();

        for (int i = 0; i < fileDatas.size(); i++) {
            File mp3File = new File(fileDatas.get(i));
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), mp3File);
            maps.put("files\"; filename=\"" + i + ".png", requestFile);
        }


        maps.put("Authorization", RequestBody.create(MediaType.parse("text/plain"), "BearereyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxODc5MjExMjc4MSIsImV4cCI6MTUyNjI5MTE5MX0.ocsu7AAWeGWYUlgIP87lhtqvLbA-KWZoqXoPneDPdunVB7dLtmXYFB5-A1o5GyNjwiyu70dQd-9_UoT-tTN5jg"));
        maps.put("type", RequestBody.create(MediaType.parse("text/plain"), "2"));
        maps.put("longitude", RequestBody.create(MediaType.parse("text/plain"), "1.99"));
        maps.put("latitude", RequestBody.create(MediaType.parse("text/plain"), "1.99"));

        maps.put("tags", RequestBody.create(MediaType.parse("text/plain"), "asdfasd,asdfasd"));

        maps.put("title", RequestBody.create(MediaType.parse("text/plain"), "你好"));
        maps.put("content", RequestBody.create(MediaType.parse("text/plain"), "测试"));
        maps.put("address", RequestBody.create(MediaType.parse("text/plain"), "图片文件测试地址"));

        maps.put("deviceNumber", RequestBody.create(MediaType.parse("text/plain"), "1213123" + ""));

        HttpMehtod.getInstance().submitWork(maps, new IdeaObserver(this) {
            @Override
            public void onNext(Object o) {

            }

            @Override
            public void onSuccess(BaseDataBean baseDataBean) {

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }
}
